﻿#region 版权说明
/**********************************************************************
      * 功能说明:
      * 
      * CLR 版本:       4.0.30319.18033
      * 类 名 称:       FmPrint
      * 命名空间:       CrmManageSystemClient.Formn.Print
      * 创建时间:       2013-05-09 16:32:05
      * 作    者:       Devil月哥
      * 版    权:       FmPrint说明：本代码版权归Devil月哥所有 All Rights Reserved (C) 2013 - 2013
      * 网    站:       http://www.xd5u.com
      * 邮    箱:       webmaster@xd5u.com
      * 唯一标识：      a4586a90-a70b-43bc-9758-4fd857bf33da  

      * 修改时间:
      * 修 改 人:
      * 
      ***********************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Collections;
using System.Xml.Linq;
using System.Reflection;

namespace CrmManageSystemClient.Formn.Print
{
    public partial class FmPrint : Form
    {
        public FmPrint()
        {
            InitializeComponent();
        }

        private IList<Logistics.Model.Model_Xd5u_Express_Info> Model_Xd5u_Express_Info;
        public FmPrint(IList<Logistics.Model.Model_Xd5u_Express_Info> _Model_Xd5u_Express_Info)
        {
            InitializeComponent();
            Model_Xd5u_Express_Info = _Model_Xd5u_Express_Info;
        }


        private void FmPrint_Load(object sender, EventArgs e)
        {
            //List<Prints.Model.ModelPrintStyle> _ModelPrintStyle = Prints.StyleHelper.GetAllStyle();

            foreach(Prints.Model.ModelPrintStyle Item in Prints.StyleHelper.GetAllStyle())
            {
                comboBox1.Items.Add(Item.StyleName);
                comboBox1.SelectedIndex = 0;
                //MessageBox.Show(Item.StyleName);
            }

            ////载入打印样式
            //MessageBox.Show(GetFiles("Reports", "*.frx").Length.ToString());
            //comboBox1.SelectedValue = "ces";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Data.DataSet _DataSet = new DataSet();

            DataTable tb = ListToDataTable<Logistics.Model.Model_Xd5u_Express_Info>(Model_Xd5u_Express_Info);
            tb.TableName = "Express";
            _DataSet.Tables.Add(tb);

            Prints.Prints.yulan(_DataSet, "Express");
        }

        public DataTable ListToDataTable<T>(IList<T> entitys)
        {

            //检查实体集合不能为空
            if (entitys == null || entitys.Count < 1)
            {
                return new DataTable();
            }

            //取出第一个实体的所有Propertie
            Type entityType = entitys[0].GetType();
            PropertyInfo[] entityProperties = entityType.GetProperties();

            //生成DataTable的structure
            //生产代码中，应将生成的DataTable结构Cache起来，此处略
            DataTable dt = new DataTable();
            for (int i = 0; i < entityProperties.Length; i++)
            {
                //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
                dt.Columns.Add(entityProperties[i].Name);
            }

            //将所有entity添加到DataTable中
            foreach (object entity in entitys)
            {
                //检查所有的的实体都为同一类型
                if (entity.GetType() != entityType)
                {
                    throw new Exception("要转换的集合元素类型不一致");
                }
                object[] entityValues = new object[entityProperties.Length];
                for (int i = 0; i < entityProperties.Length; i++)
                {
                    entityValues[i] = entityProperties[i].GetValue(entity, null);

                }
                dt.Rows.Add(entityValues);
            }
            return dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
